function OnContinueClick() {
    $.GetContextPanel().style.visibility="collapse"
}

(function(Global /*, context */) {

    var ranking=0;
    var isShown=false;

    function SetData(data) {
        // $.Msg(data)
        for(var i in data){


            var item = $("#TeamsContainer").FindChildTraverse("item"+i);
            if(item===null) {
                item =$.CreatePanel( "Panel", $("#TeamsContainer"), "item"+i );
                item.BLoadLayout( "file://{resources}/layout/custom_game/end_screen_item.xml", false, false );
            }
            if(Number(data[i].playerId)===Players.GetLocalPlayer()){
                ranking=i
                $("#EndScreenVictoryLeft").text=$.Localize("#UI_EndScreen_No")+i;
                $("#EndScreenVictoryRight").text="/"+Game.GetAllPlayerIDs().length;

                item.SetHasClass("LocalPlayer",true);
            }
            item.index=i;
            item.SetItemData(data[i])
        }
    }


    function OnEndGameChanged(table, key, data) {
        $.Msg("OnEndGameChanged...",key)
        if (key === String(Players.GetLocalPlayer())) {
                Show(data)
        }
        else if (key==="all"){
            if(Game.GetGameWinner()!==-1){
                Show(data)
            }
        }
    }



    function UpdateScreenData() {
        var tableData = CustomNetTables.GetTableValue("end_game", "all");
        if(tableData){
            Show(tableData)
        }
        $.Schedule(1,UpdateScreenData);
    }

    function Show(data){
        if(isShown===false){
            isShown=true
        }
        else{
            SetData(data);
            return
        }
        Game.EmitSound("valve_ti5.stinger.dire_win")
        SetData(data)
        $("#EndScreenRoot").style.visibility="visible"
        $("#EndScreenRoot").SetHasClass("EndScreenAnim",true);
        $("#VictoryRow").SetHasClass("VictoryRowAnim",true);

        if (Players.GetTeam(Players.GetLocalPlayer())===Game.GetGameWinner()){
            $.Schedule(4, function () {
                Game.EmitSound("announcer_ann_custom_end_02");
                $.Schedule(1.5, function () {
                    Game.EmitSound("announcer_ann_custom_place_01")
                })
            });
        }
        else if(Number(ranking)!==1){
            // var ranking=ranking;
            $.Schedule(4, function () {
                    Game.EmitSound("announcer_ann_custom_end_10");
                    $.Schedule(1, function () {
                        Game.EmitSound("announcer_ann_custom_place_0"+ranking)
                    })
            });
        }
    }
    var tableData = CustomNetTables.GetTableValue("end_game", "all");
    if(tableData){
        $("#ContinueButton").style.visibility="collapse";
        Show(tableData)
        $.Schedule(0.1,UpdateScreenData);
    }

    CustomNetTables.SubscribeNetTableListener("end_game", OnEndGameChanged);


})(GameUI.CustomUIConfig(), this);